require(['init'], function () {
  var $pagination = $('#js-pagination')
  var pagination
   //此页面的删除是假删除
    var url="";
    //getmarketing()//获取营销的列表
    function getmarketing(querystr, cb){
      var url = '/api/marketing/posts';
      var query = {}
      if(querystr) {
        query = querystr
      }
        ajaxMethod({
          url: url,
          data: query,
          method:"get",
          success:function(data){
            cb && cb(data)
            if(data.list.length == 0) {
              $('#marketing_agencylist').html('<div style="margin-top: 50px;text-align: center;">'+i18next.t('input.nullresults')+'</div>');
              $pagination.hide()
              return;
            }
            readermarketing(data);
          }
        })
    }
    function readermarketing(data){
      var datalist=data.list;
      var $marketing_agencylist=$('#marketing_agencylist').html('');
      $.each(datalist,function(index,item){
         if(item.status==1||item.status==2){
         var $tr=$('<tr class="enroll"></tr>').appendTo($marketing_agencylist);
         var id=item.id;
        console.log(item.id)
         //url="/articles/article.html?id="+id+"&posttitle="+item.title+"";
          $tr.append('<td>'+(index+1)+'</td>');
         var $c_td=$('<td></td>').appendTo($tr);
         var $a=$('<a href="/articles/article.html?id='+id+'&posttitle='+item.title+'" target="_blank">'+item.title+'</a>').appendTo($c_td);
         var $c_url=$('<td><a href="/articles/article.html?id='+id+'&posttitle='+item.title+'" target="_blank">'+item.url+'</a></td>').appendTo($tr);
         $tr.append('<td>'+item.updated_at+'</td>');
         $tr.append('<td>'+item.updated_by+'</td>');
         var $tdststus=$('<td></td>').appendTo($tr);
         $tr.append('<td>'+item.created_at+'</td>');
         $tr.append('<td>'+item.created_by+'</td>');
         var $tdoperation=$('<td></td>').appendTo($tr);
          $a.hover(function(){
           $(this).addClass("c_hover")
          },function(){
           $(this).removeClass("c_hover")
          })
          $c_url.hover(function(){
           $(this).find("a").addClass("c_hover")
           $(this).addClass("c_hover")
         },function(){
           $(this).find("a").removeClass("c_hover")
           $(this).removeClass("c_hover")
          })
          if(item.status==1){
            var $spa_stno=$('<span>'+i18next.t('table.noshare')+'</span>').appendTo($tdststus);
            var $sp_start=$('<span class="btn btn-primary btn-sm btn-inverse" style="width:96px!important;">'+i18next.t('table.sharewechat')+'</span>').appendTo($tdoperation);
            $sp_start.on('click',function(){
              var arr={
                title:item.title,
                image:item.cover,
                description:item.description,
                url:item.url,
                id:item.id
              }
              $.ajax({
                url:'/wechat/news',
                data:arr,
                type:'post',
                success:function(data){
                    var popUpBox = new PopUpBox({
                      type: 'dom',
                      style: 'default contentScroll',
                      submitBtnType: 'alert',
                      titleText: ''+i18next.t('popupbox.systemmessages')+'',
                      show: true,
                      content: "<div style='margin-left:27px;margin-top:34px;'>"+data.status.message+"</div>",//这里的删除是假删除
                      callback: {
                        yes: function(e) {
                          //window.location.reload();
                        }
                      },
                      position: {
                        width: 448,
                        height: 226
                      }
                    })
                    $('.submitBtnBox').find('.btn').css('margin-align','-275px');
                 }
              })
           })
          }else if(item.status==2){
            var $spa_stno=$('<span>'+i18next.t('table.share')+'</span>').appendTo($tdststus);
            var $sp_no=$('<span class="btn btn-primary btn-sm btn-inverse" style="width:96px!important;"disabled="disabled">'+i18next.t('table.share')+'</span>').appendTo($tdoperation);
          }
          var $edit=$('<a class="btn btn-primary btn-sm btn-inverse" style="margin:0 6px"  href="marketing_agency.html?id='+id+'">'+i18next.t('table.compile')+'</a>').appendTo($tdoperation);
          var $delete=$('<span class="btn btn-danger btn-sm btn-inverse">'+i18next.t('table.delete')+'</span>').appendTo($tdoperation);
          $delete.on('click',function(){
            var popUpBox = new PopUpBox({
              type: 'dom',
              style: 'default contentScroll',
              submitBtnType: 'confirm',
              titleText: ''+i18next.t('popupbox.warmprompt')+'',
              show: true,
              content: $('<div style="margin-left:27px;margin-top:34px;">'+i18next.t('popupbox.del')+'</div>'),//这里的删除是假删除
              callback: {
                yes: function(e) {
                  $.ajax({
                    url:'/api/marketing/post/'+id+'/disable',
                    method:'put',
                    success:function(data){
                      window.location.reload();
                    }
                })
                }
              },
              position: {
                width: 448,
                height: 226
              }
            })
          })
        }
      })
      $pagination.show();
    }

    function loadSearchData(querystr) {
      if(querystr) {
        pagination.filter.title = querystr
        pagination.searchData()
      } else {
        pagination.reloadData()
      }
    }

  $('#J_marketing').on('click', function() {
    var $input = $(this).prev();
    var querystr = $.trim($input.val());
    loadSearchData(querystr)
  })

  $('.searchInput').on('keyup', function(e) {
    if(e.keyCode == 13) {
      var querystr =$.trim($(this).val());
      loadSearchData(querystr)
    }
  })
  pagination = new Pagination($pagination, getmarketing)
})
